const mysql = require('mysql')
let db = {}
let dbOption = {
    host: 'localhost',
    user: 'root',
    password: 'root',
    database: 'daily',
    port: 3306
}

// 插入操作
db.query = (connection, sql, params, callback) => {
    connection.query(sql, params, (err, results, fields) => {
        if (err) {
            throw err;
        }
        callback(results.insertId)
    })
}

// 关闭数据库
db.close = (connection) => {
    connection.end((err) => {
        if (err) {
            return false
        }
        console.log('数据库连接关闭！')
    })
}

// 获取数据库连接
db.connection = () => {
    let connection = mysql.createConnection(dbOption)
    connection.connect((err) => {
        if (err) {
            console.log(err)
            return false
        }
    })
    return connection
}

module.exports = {
    db: db,
    dbOption: dbOption
}
